package main;

import java.awt.Color;
import java.awt.Graphics;
import java.math.*;

public class Car {
	float xSpeed, ySpeed, x, y, direction, angleChange, velocity;
	static final int LENGTH = 20;
	public Car(int x, int y){
		angleChange = 0;
		xSpeed = ySpeed = 0;
		this.x = x;
		this.y = y;
		direction = 0;
	}
	
	public double div(double over, double under){
		if (under == 0){
			return 1;
		}
		return over/under;
	}
	
	public void accelerate(){
		xSpeed += Math.sin(direction)*velocity;
		ySpeed += Math.cos(direction)*velocity;
	}
	
	public void draw(Graphics g, Manager m){
		accelerate();
		x += xSpeed;
		y += ySpeed;
		direction += angleChange*(Math.sqrt(Math.pow(xSpeed, 2)+Math.pow(ySpeed, 2)))*0.05*div(Math.abs(velocity),(velocity));
		if (x > m.WIDTH){
			x = m.WIDTH;
			xSpeed = 0;
		}else if (x < 0){
			x = 0;
			xSpeed = 0;
		}
		if (y > m.HEIGHT){
			y = m.HEIGHT;
			ySpeed = 0;
		}else if (y < 0){
			y = 0;
			ySpeed = 0;
		}
		if (angleChange > 1){
			angleChange = 1;
		}else if (angleChange < -1){
			angleChange = -1;
		}
//		xSpeed -= Math.sin(direction)*.02*xSpeed;
//		ySpeed -= Math.cos(direction)*.02*ySpeed;
		xSpeed *= .98;
		ySpeed *= .98;
//		xSpeed += Math.sin(direction + 0.02 * Math.PI)*.5*xSpeed;
//		ySpeed += Math.cos(direction + 0.02 * Math.PI)*.5*ySpeed;
//		xSpeed -= Math.sin(direction - 0.02 * Math.PI)*.5*xSpeed;
//		ySpeed -= Math.cos(direction - 0.02 * Math.PI)*.5*ySpeed;
		velocity = 0;
		angleChange *= .98;
		g.setColor(Color.black);
		g.drawLine((int)(x), (int)(y), (int)(x-(LENGTH)*Math.sin(direction)), (int)(y-(LENGTH)*Math.cos(direction)));
		g.drawLine((int)(x), (int)(y), (int)(x-(LENGTH/2)*Math.sin((direction + angleChange) - Math.PI * .25)), (int)(y-(LENGTH/2)*Math.cos((direction + angleChange) - Math.PI * .25)));
		g.drawLine((int)(x), (int)(y), (int)(x-(LENGTH/2)*Math.sin((direction + angleChange) + Math.PI * .25)), (int)(y-(LENGTH/2)*Math.cos((direction + angleChange) + Math.PI * .25)));
	}
}
